//
//  AY_BookInviteAlertView.m
//  AiYue
//
//  Created by boWen on 2023/11/13.
//

#import "AY_BookInviteAlertView.h"

@interface AY_BookInviteAlertView ()

@end

@implementation AY_BookInviteAlertView


- (instancetype)initWithFrame:(CGRect)frame {
    if([super initWithFrame:frame]) {
        [self initWithAllUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

#pragma mark 监听键盘的显示与隐藏
///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.frame = CGRectMake(self.frame.origin.x, -50, SCREEN_W, SCREEN_H);
    }];
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notification {
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.frame = CGRectMake(self.frame.origin.x, 0, SCREEN_W, SCREEN_H);
    }];
}

- (void)initWithAllUI {
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    UIView * bgView = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor clearColor] andRadius:YNUM(16) andAddSubView:self];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
        make.height.mas_offset(YNUM(279));
        make.width.mas_offset(YNUM(306));
    }];
    
    UIImageView * bgImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_xinren_alert.png") andBg:[UIColor clearColor] andRadius:YNUM(12) andAddSubView:bgView];
    bgImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLingqAction)];
    [bgImgView addGestureRecognizer:tap];
    [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView);
        make.right.equalTo(bgView);
        make.left.equalTo(bgView);
        make.height.mas_offset(YNUM(279));
    }];
    
    UIView * fieldBg= [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"#000000") andRadius:YNUM(16) andAddSubView:bgImgView];
    [fieldBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgImgView).offset(YNUM(-64));
        make.centerX.equalTo(bgImgView);
        make.height.mas_offset(YNUM(40));
        make.width.mas_offset(YNUM(240));
    }];
    
    self.inviteField = [[AY_BookHolder alloc]init];
    self.inviteField.placeholder = @"输入邀请码";
    self.inviteField.font = [UIFont systemFontOfSize:YNUM(14)];
    self.inviteField.textAlignment = NSTextAlignmentCenter;
    self.inviteField.textColor = [UIColor whiteColor];
    [fieldBg addSubview:self.inviteField];
    [self.inviteField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(fieldBg);
    }];
    
    UIButton* closeBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:nil andTitleColor:nil andBg:nil andImage:YImage(@"closeIcon.png") andBgImage:nil andTarget:self andAction:@selector(dismissPopView) andRadius:0.0 andAddSubView:self];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgImgView.mas_bottom).offset(YNUM(30));
        make.centerX.equalTo(self);
        make.height.width.mas_offset(YNUM(34));
    }];
}


// 显示View
- (void)showPopView{
    [[[UIApplication sharedApplication] windows].firstObject addSubview:self];
}

// 隐藏View
- (void)dismissPopView{
    if(self) {
        [self removeFromSuperview];
    }
}

- (void)handleLingqAction {
    if(kStringIsEmpty(self.inviteField.text)) {
        ShowToast(@"请填写邀请码");
        return;
    }
    [self dismissPopView];
    if(self.inviteBlock) {
        self.inviteBlock();
    }
}

@end
